﻿Imports Runa.Foundation
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics

Namespace Global.Runa.Resources.ResourceManager

    Public Class FontObject
        Inherits ResourceObjectBase

        Public Property Size() As Integer
        Public Property Color() As Color
        Public Property Font() As SpriteFont

        Public Sub New()
            MyBase.New()
            Me.Size = 0
            Me.Color = Color.Black
        End Sub

        Public Sub New(ByVal name As String, ByVal fontFileName As String)
            MyBase.New(name, fontFileName)
            Me.Size = 22
            Me.Color = Color.Black
        End Sub

        Public Sub New(ByVal name As String, ByVal fontFileName As String, ByVal g As Game)
            Me.New(name, fontFileName)
            Me.Load(g)
        End Sub

        Public Sub New(ByVal name As String, ByVal fontFileName As String, ByVal spf As SpriteFont, Optional ByVal fontSize As Integer = 22)
            MyBase.New(name, fontFileName)
            Me.Font = spf
            Me.Size = fontSize
            Me.Color = Color.Black
            Me.IsLoaded = True
        End Sub

        Public Sub New(ByVal name As String, ByVal fontFileName As String, ByVal spf As SpriteFont, ByVal color As Color, Optional ByVal fontSize As Integer = 22)
            MyBase.New(name, fontFileName)
            Me.Font = spf
            Me.Size = fontSize
            Me.Color = color
            Me.IsLoaded = True
        End Sub

        Public Overrides Function Load(g As Game) As Boolean
            If Me.IsLoaded Then Return True
            Me.Font = g.Content.Load(Of SpriteFont)(Me.FileName)
            Return True
        End Function

        Public Function MeasureString(ByVal text As String, ByVal rect As Rectangle) As List(Of Tuple(Of Char, Vector2))
            Dim lst As New List(Of Tuple(Of Char, Vector2))
            Dim font = Me
            Dim totalSize = font.Font.MeasureString(text)
            If totalSize.X > rect.Width Then
                Dim lines = CInt(totalSize.X / rect.Width + 0.5)
                Dim charCountPerLine =
                    Function()
                        Dim chCount As Integer = 0
                        Dim deltaX As Integer = 0
                        While deltaX < rect.X
                            deltaX += font.Font.MeasureString(text(chCount)).X
                            chCount += 1
                        End While
                        Return chCount
                    End Function()
                Dim posX = 0
                Dim posY = 0
                Dim line = 0
                For i As Integer = 0 To text.Length - 1
                    Dim count = i + 1
                    Dim c = text(i)
                    Dim index = count Mod charCountPerLine
                    Dim v = font.Font.MeasureString(c.ToString())
                    If index = 0 Then
                        line += 1
                        posX = 0
                    Else
                        posX += v.X
                    End If
                    posY = v.Y * line
                    lst.Add(New Tuple(Of Char, Vector2)(c, New Vector2(posX, posY)))
                Next
            Else
                Dim posX As Integer = 0
                Dim posY As Integer = 0
                For Each c In text
                    Dim v = font.Font.MeasureString(c.ToString())
                    posX += v.X
                    lst.Add(New Tuple(Of Char, Vector2)(c, New Vector2(posX, posY)))
                Next
            End If
            Return lst
        End Function

        Public Shared Narrowing Operator CType(ByVal fo As FontObject) As SpriteFont
            Return fo.Font
        End Operator

    End Class

End Namespace